﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using Luna.WPF.ApplicationFramework;
using System.Windows.Controls;
using Caliburn.Core.Metadata;
using System.IO;

namespace XWord.GUI.Presenters.Fundamental
{
    [View(typeof(WebBrowser))]
    [PerRequest(typeof(WebBrowserPresenter))]
    public class WebBrowserPresenter : DockingPresenter
    {

        public Uri Source { get; set; }

        public void Show(string title, Uri source)
        {
            this.DisplayName = title;
            this.Source = source;
            this.Show();
        }

        public void ShowLocal(string title, string source)
        {
            this.DisplayName = title;
            var reader = new StreamReader(source);
            sourceString = reader.ReadToEnd();
            reader.Dispose();
            this.Show();
        }

        private string sourceString;
        private bool _isLoaded;
        public override void ViewLoaded(object view, object context)
        {
            if (!_isLoaded)
            {
                WebBrowser element = view as WebBrowser;
                if (string.IsNullOrEmpty(sourceString))
                    element.Source = Source;
                element.NavigateToString(sourceString);
                _isLoaded = true;
            }
            base.ViewLoaded(view, context);
        }
    }
}
